package com.yanqu.road.dao.impl.activity.suoyao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suoyao.SuoYaoNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SuoYaoNoticeDaoImpl extends TempDataObjectDao<SuoYaoNotice> {

    public static String getSuoYaoNoticeListSql = "select * from t_c_activity_suoyao_notice where activityId=? order by `time` asc";

    public List<SuoYaoNotice> getSuoYaoNoticeList(int activityId) {
        List<SuoYaoNotice> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSuoYaoNoticeListSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SuoYaoNotice suoYaoNotice = parse(rs);
                list.add(suoYaoNotice);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    @Override
    protected boolean updateData(SuoYaoNotice data) {
        return false;
    }

    public static String insertSuoYaoNoticeSql = "insert into t_c_activity_suoyao_notice(activityId,userId,userBaseInfo,type,rewardId,time) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(SuoYaoNotice suoYaoNotice) {
        suoYaoNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoNotice.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, suoYaoNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoNotice.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoNotice.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoNotice.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, suoYaoNotice.getTime()));
        if (getDb().executeNoneQuery(insertSuoYaoNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected SuoYaoNotice parseData(ResultSet rs) throws SQLException {
        SuoYaoNotice suoYaoNotice = new SuoYaoNotice();
        suoYaoNotice.setActivityId(rs.getInt("activityId"));
        suoYaoNotice.setUserId(rs.getLong("userId"));
        suoYaoNotice.setUserBaseInfo(JSONObject.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        suoYaoNotice.setType(rs.getInt("type"));
        suoYaoNotice.setRewardId(rs.getInt("rewardId"));
        suoYaoNotice.setTime(rs.getLong("time"));
        return suoYaoNotice;
    }
}
